package com.rpframework.website.friendly.service;

import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.aliyun.oss.common.utils.DateUtil;
import com.rpframework.core.BaseService;
import com.rpframework.utils.DateUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.friendly.dao.INewsDao;
import com.rpframework.website.friendly.domain.News;
import com.rpframework.website.friendly.util.HConstants;
import com.rpframework.website.friendly.domain.News;

@Service
public class NewsService extends BaseService{
public @Resource INewsDao iDao;
	
	public Pager<News> getPager(Pager<News> pager) {
		long startTime = System.currentTimeMillis();
		List<News> list = iDao.doPager(this.packageMyBatisParam(pager));
		pager.setItemList(list);
		pager.setCostTime(System.currentTimeMillis() - startTime);
		return pager;
	}

	public boolean doSaveOrUpdate(News t){
		if(NumberUtils.isValid(t.getId())){
			if(t.getUrlList()==null){
				News DBEntity = iDao.select(t.getId());
				t.setUrlList(DBEntity.getUrlList());
			}
			return iDao.update(t);
		}
		return insert(t);
	}

	/**
	 * 修改状态
	 * @param id
	 * @return
	 * @time 2016年3月24日 上午10:02:46
	 */
	public boolean doUpdateState(Integer id) {
		if(NumberUtils.isValid(id)){
			News t = iDao.select(id);
			if(t.getState() == HConstants.EntityState.STATE_NORMAL){
				t.setState(HConstants.EntityState.STATE_DISABLE);
			}else{
				t.setState(HConstants.EntityState.STATE_NORMAL);
			}
			return iDao.update(t);
		}
		return false;
	}

	public boolean getRecommender(Integer id) {
		if(NumberUtils.isValid(id)){
			News t = iDao.select(id);
			if(NumberUtils.isValid(t.getType())){
				t.setType(0);
			}else{
				t.setType(1);
			}
			return iDao.update(t);
		}
		return false;
	}
	/**
	 * 查推荐的   查询条数为 num条
	 * @param num
	 * @return
	 */
	public List<News> getRecommenderByNum(Integer num) {
		
		return iDao.doRecommenderByNum(num);
	}
	/**
	 * 查最新的新闻   查询条数为 num条
	 * @param num
	 * @return
	 */
	public List<News> getNewByNum(Integer num) {
		
		return iDao.doNewByNum(num);
	}

	public Integer getUpId(Integer id) {
		return iDao.doUpId(id);
	}

	public Integer getNextId(Integer id) {
		return iDao.doNextId(id);
	}
	public Integer getMaxId() {
		return iDao.doMaxId();
	}
	public Integer getMinId() {
		return iDao.doMinId();
	}
}
